﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebLibSolPedBE;
using SAP.Middleware.Connector;
using LibSolPedI;

namespace LibSolPedDALC
{
    public class LiberaReservaDALC : ILiberaReserva
    {
        private LiberaReservaDALC() { }
        private static volatile LiberaReservaDALC _instance = null;

        public static LiberaReservaDALC Instance()
        {
            if (_instance == null)
            {
                lock (typeof(LiberaReservaDALC))
                {
                    if (_instance == null)
                    {
                        _instance = new LiberaReservaDALC();
                    }
                }
            }
            return _instance;
        }

        public List<ReservaBE> getLstReserva()
        {
            List<ReservaBE> lst = new List<ReservaBE>();

            IRfcFunction function = null;
            RfcDestination dest = null;
            DestConfig dc = new DestConfig();
            try
            {
                function = DALC.rfcRep.CreateFunction("ZMOVIL_MM_CONSULTA_RESERVAS");

                function.Invoke(DALC.rfcDest);
                IRfcTable T_RETORNO = function.GetTable("T_RETORNO");
                foreach (IRfcStructure item in T_RETORNO)
                {
                    ReservaBE it = new ReservaBE();
                    it.RSNUM = item["RSNUM"].GetString();
                    it.RSPOS = item["RSPOS"].GetString();
                    it.MATNR = item["MATNR"].GetString();
                    it.BDMNG = item["BDMNG"].GetString();
                    it.MEINS = item["MEINS"].GetString();
                    it.WERKS = item["WERKS"].GetString();
                    it.USNAM = item["USNAM"].GetString();
                    it.KOSTL = item["KOSTL"].GetString();
                    it.MAKTX = item["MAKTX"].GetString();
                    lst.Add(it);
                }
            }
            catch (Exception e)
            {

            }

            return lst;
        }

        public string setLiberaReserva(string IP_NRORESERV, string IP_POSRESERV)
        {
            string ret = "";
            IRfcFunction function = null;
            RfcDestination dest = null;
            DestConfig dc = new DestConfig();
            try
            {
                function = DALC.rfcRep.CreateFunction("ZMOVIL_MM_LIBERA_RESERVAS");
                function.SetValue("IP_NRORESERV", IP_NRORESERV);
                function.SetValue("IP_POSRESERV", IP_POSRESERV);
                function.Invoke(DALC.rfcDest);
                IRfcTable T_RETORNO = function.GetTable("T_RETORNO");
                foreach (IRfcStructure item in T_RETORNO)
                {
                    ret += item["MESSAGE"].GetString() + ". ";
                }
            }
            catch (Exception e)
            {
                ret = e.Message;
            }

            return ret;
        }
    }
}
